from pymongo import MongoClient
from urllib import parse
# 数据库链接，必须保证当前系统能正常访问mongodb！！!
"""方式1：如果账号密码没有特殊字符"""
# connect = MongoClient("mongodb://mofang:123456@127.0.0.1:27017/mofang")

""" 方式2 当前数据库的管理员链接"""
# username = parse.quote_plus("mofang")	# 对用户名进行编码
# password = parse.quote_plus("123456")	# 对密码进行编码
# database = parse.quote_plus("mofang")	# 对密码进行编码
# connect = MongoClient("mongodb://%s:%s@127.0.0.1:27017/%s" % (username,password,database))
# my_db = connect['mofang']
# my_collections = my_db['my_collections']
# print(my_collections)
# data = {"name":"xiaoming2号","sex":True,"age":17}
# ret = my_collections.insert_one(data)


""" 方式3 基于超级管理员身份链接,不需要当前库的管理员了 """
# username = parse.quote_plus("root")	# 对用户名进行编码
# password = parse.quote_plus("123")	# 对密码进行编码
# database = parse.quote_plus("admin") #数据库名称
# host = "127.0.0.1"
# port = "27017"
# mongo = MongoClient("mongodb://%s:%s@%s:%s/%s" % (username,password,host,port,database))
# my_db = mongo["mofang"]
# print(mongo.list_database_names())
# my_collections = my_db["my_collections"]
# my_collections.delete_many({})
#  # 添加一篇文档
# data = {"name":"xiaoming1号","sex":True,"age":17}
# ret = my_collections.insert_one(data)
# print(ret)






